/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.payment.service;

import java.util.List;

import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.order.entity.serviceorder.ServiceOrderDetail;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.payment.entity.Tap;
import com.jeesite.modules.payment.dao.TapDao;
import com.jeesite.modules.payment.entity.TapDetail;
import com.jeesite.modules.payment.dao.TapDetailDao;

/**
 * 应付单Service
 * @author mr
 * @version 2021-03-11
 */
@Service
@Transactional(readOnly=true)
public class TapService extends CrudService<TapDao, Tap> {
	
	@Autowired
	private TapDetailDao tapDetailDao;
	@Autowired
	private TapDao tapDao;
	@Autowired
	private CodeRuleService codeRuleService;
	/**
	 * 获取单条数据
	 * @param tap
	 * @return
	 */
	@Override
	public Tap get(Tap tap) {
		Tap entity = super.get(tap);
		if (entity != null){
			TapDetail tapDetail = new TapDetail(entity);
			tapDetail.setStatus(TapDetail.STATUS_NORMAL);
			entity.setTapDetailList(tapDetailDao.findList(tapDetail));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param tap 查询条件
	 * @return
	 */
	@Override
	public Page<Tap> findPage(Tap tap) {
		return super.findPage(tap);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param tap
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Tap tap) {
		if(tap.getIsNewRecord()){
			tap.setApCode(codeRuleService.getCode("investorcarcontract001"));
			tap.setCreateBy(UserUtils.getUser().getLoginCode());
			//设置审批状态
			tap.setBillStatus("1");
			//设置单据状态
			tap.setStatus1("1");
		}
		super.save(tap);

		if (tap.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("investorcarcontract001");
		}
		// 保存 Tap子表
		for (TapDetail tapDetail : tap.getTapDetailList()){
			if (!TapDetail.STATUS_DELETE.equals(tapDetail.getStatus())){
				tapDetail.setApId(tap);
				if (tapDetail.getIsNewRecord()){
					tapDetail.setPayLeft(tapDetail.getApAmount());
					tapDetailDao.insert(tapDetail);
				}else{
					tapDetailDao.update(tapDetail);
				}
			}else{
				tapDetailDao.delete(tapDetail);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param tap
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Tap tap) {
		super.updateStatus(tap);
	}
	
	/**
	 * 删除数据
	 * @param tap
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Tap tap) {
		super.delete(tap);
		TapDetail tapDetail = new TapDetail();
		tapDetail.setApId(tap);
		tapDetailDao.deleteByEntity(tapDetail);
	}

	/**
	 * 查询车架号
	 * @param serviceOrderId
	 * @return
	 */
	@Transactional(readOnly=false)
	public List<ServiceOrderDetail> getFrameNumber(String serviceOrderId){
		List<ServiceOrderDetail> frameNumberList = tapDao.getFrameNumber(serviceOrderId);
		return frameNumberList;
	}

	/**
	 * 获取车辆信息
	 * @param frameNumber
	 * @return
	 */
	@Transactional(readOnly=false)
	public ServiceOrderDetail getCarDetailInfo(String frameNumber){
		ServiceOrderDetail carDetailInfo = tapDao.getCarDetailInfo(frameNumber);
		return carDetailInfo;
	}

	/**
	 * 财务确认
	 * @param apId
	 * @return
	 */
	@Transactional(readOnly=false)
	public int updateFinancialRecognition(String apId,String sta){
		int i=0;
		if("5".equals(sta)){
			Tap tap =new Tap();
			tap.setStatus1(sta);
			tap.setApId(apId);
			tapDao.update(tap);
			i=1;
		}else {
			 i = tapDao.updateFinancialRecognition(apId,sta);
			if("3".equals(sta)){
				tapDao.updateStatu(apId);
		}

		}
		return i;
	}


	/**
	 * 获取一个应付详情
	 * @param id
	 * @return
	 */
	@Transactional(readOnly=false)
	public TapDetail findById(String id){
		TapDetail tapDetail = tapDetailDao.findById(id);
		return tapDetail;
	}
/**
 * 获取一个应付单号
 * @param id
 * @return
 */
@Transactional(readOnly=false)
public Tap selectOne(String id){
	Tap tap= tapDao.selectOne(id);
	return tap;
}
}